#include "stdafx.h"

#include "LinkList.h"

#ifdef RUN_2_5

LinkList addList(LinkList& num1, LinkList& num2)
{
	Node* pt1 = num1.head;
	Node* pt2 = num2.head;
	if(!pt1 || !pt2)
	{
		return LinkList();
	}

	LinkList num3;
	Node *head = NULL, *tail, *pt;
	int flag = 0;
	int tmp;
	while(pt1 && pt2)
	{
		tmp = pt1->data + pt2->data + flag;
		if(tmp >= 10)
		{
			pt = new Node(tmp - 10);
			flag = 1;
		}
		else
		{
			pt = new Node(tmp);
			flag = 0;
		}

		if(head == NULL)
		{
			head = pt;
			tail = pt;
		}
		else
		{
			tail->next = pt;
			tail = pt;
		}
		pt1 = pt1->next;
		pt2 = pt2->next;
	};
	
	if(!pt1 && !pt2)
	{
		if(flag)
		{
			pt = new Node(1);
			tail->next = pt;
		}
		num3.head = head;
		return num3;
	};
	
	pt1 = pt1 ? pt1 : pt2;
	while(pt1)
	{
		tmp = pt1->data + flag;
		if(tmp >= 10)
		{
			pt = new Node(tmp - 10);
			flag = 1;
		}
		else
		{
			pt = new Node(tmp);
			flag = 0;
		}
		
		tail->next = pt;
		tail = pt;

		pt1 = pt1->next;
	};

	if(flag)
	{
		pt = new Node(1);
		tail->next = pt;
	}
	num3.head = head;
	return num3;

}


int main()
{
	LinkList num1;
	LinkList num2;

	num1.appendToTail(7);
	num1.appendToTail(8);
	num1.appendToTail(9);

	num2.appendToTail(7);
	num2.appendToTail(7);
	num2.appendToTail(7);
	num2.appendToTail(9);
	num2.appendToTail(9);
	
	cout << "Num1" << endl;
	num1.print();
	cout << "Num2" << endl;
	num2.print();

	cout << "After addition" << endl;
	LinkList num3 =  addList(num1, num2);
	num3.print();

    cin;

}

#endif
